package com.mgface.thymeleaf.exception.resolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;

import com.mgface.thymeleaf.exception.ThymeleafException;
import com.mgface.thymeleaf.util.SwissKnife;

/**
 * 自定义异常解析器
 * 
 * @author yuxiang
 *
 */
public class ThymeleafCustomExceptionResolver extends SimpleMappingExceptionResolver {
	@Override
	protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
			Exception ex) {
		ModelAndView mv = super.doResolveException(request, response, handler, ex);
		if (mv != null) {
			if(ThymeleafException.class.isAssignableFrom(ex.getClass())){
				String basePath = SwissKnife.getBasePath(request, response);
				ThymeleafException ts = (ThymeleafException)(ex);
				mv.addObject("context", basePath);
				mv.addObject("code", ts.getCode());
				mv.addObject("cause",ts.getDescription());
				mv.addObject("detailMessage", ts);
			}
		}
		return mv;
	}
}
